package tk.xboot.boot.conf;


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import tk.xboot.boot.event.CustomEvent;
import tk.xboot.boot.event.EventBean;

@Slf4j
@Configuration
public class EventListenerConf {

    // Handle all event or filter the specified event.
    @EventListener
    public void handleEvent(Object event){
        // filter the specified event.
        //if(event instanceof CustomEvent){
        //}
        log.info("EventBean：{}", event);
    }

    // Listen all CustomEvent
    @EventListener
    public void handleCustomEvent(CustomEvent customEvent) {
        log.info("Catch CustomEvent, message:{}, event publish time {}",
                customEvent.getEventBean().getMessage(),customEvent.getTimestamp());
    }

    // Listen filtered CustomEvent by condition.
    @EventListener(condition = "#customEvent.eventBean.code == 'sign in'")
    public void handleCustomEventByCondition(CustomEvent customEvent) {
        log.info("Catch custom event with sign in, message:{}, event publish datetime {}"
        ,customEvent.getEventBean().getMessage(),customEvent.getTimestamp());
    }

    // The same with event bus post which impl in google guava.
    @EventListener
    public void handleObjectEvent(EventBean eventBean) {
        log.info("Catch object event, message:{}", eventBean.getMessage());
    }
}
